//
//  HYCTourPalController.m
//  FamilyTravel
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCTourPalController.h"
#import "HYCHeadDocument.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HYCTourPalController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) NSMutableArray *secArr;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, retain) MBProgressHUD *hud;
@end

@implementation HYCTourPalController
- (void)dealloc {
    [_tableView release];
    [_arr release];
    [_secArr release];
    [_hud release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *beanTripBackgroundImage=[UIImage imageNamed:@"tou.jpg"];
    [self.navigationController.navigationBar setBackgroundImage:beanTripBackgroundImage forBarMetrics:UIBarMetricsDefault];
    
    self.arr = [NSMutableArray array];
    self.secArr = [NSMutableArray array];
    self.page = 1;
    self.title = @"驴友分享";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 70) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    //    self.tableView.rowHeight = 150;
    //横岗去掉
    self.tableView.separatorStyle = NO;
    
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    
    [self netWorkingJudge];
    
}

- (void)netWorkingJudge {
    AFNetworkReachabilityManager *judge = [AFNetworkReachabilityManager sharedManager];
    [judge startMonitoring];
    [judge setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status == 1 || status == 2) {
            [self createData];
            
            // 上拉刷新下拉加载
            self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HeaderAction)];
            
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                // 进入刷新状态后会自动调用这个block
                self.page++;
                [self createData];
            }];
        }else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"没有网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alertView show];
        }
        
    }];
    
}



// 刷新
-(void)HeaderAction{
    [self.arr removeAllObjects];
    [self createData];
}



- (void)createData {
    
    NSString *tStr = [NSString stringWithFormat:@"http://tubu.ibuzhai.com/rest/v1/travelog/recommends?app_version=2.4.2&device_type=1&page=1&page_size=%ld", self.page * 20];
    NSURL *url = [NSURL URLWithString:tStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.arr = [HYCTourPalModel baseModelByArr:dic[@"logs"]];
        [self.hud removeFromSuperview];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    HYCTourPalCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[HYCTourPalCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    if (self.arr.count != 0) {
        HYCTourPalModel *model = self.arr[indexPath.row];
        cell.model = model;
        
        [cell.autImageView sd_setImageWithURL:[NSURL URLWithString:model.created_by[@"avatar"]]placeholderImage:[UIImage imageNamed:@"zw.png"]];
        cell.addLabel.text = model.created_by[@"nickname"];
        // cell动画
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
        
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.arr.count != 0) {
        HYCTourPalPushtroller *pRVC = [[HYCTourPalPushtroller alloc] init];
        pRVC.hidesBottomBarWhenPushed = YES;
        
        pRVC.str = [self.arr[indexPath.row] number];
        [self.navigationController pushViewController:pRVC animated:YES];
        pRVC.title = [self.arr[indexPath.row] name];
        pRVC.model = self.arr[indexPath.row];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
